package com.sctvcloud.dongqu.ui.adapter;

import android.content.Context;
import android.content.Intent;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.ruihang.generalibrary.ui.adapter.BaseHolderAbsAdapter;
import com.ruihang.generalibrary.ui.util.IListData;
import com.ruihang.generalibrary.ui.util.OnItemInternalClick;
import com.sctvcloud.dongqu.R;
import com.sctvcloud.dongqu.beans.AddrInfo;
import com.sctvcloud.dongqu.ui.activities.AddressAddActivity;
import com.sctvcloud.dongqu.ui.adapter.holder.AddressAddHolder;
import com.sctvcloud.dongqu.ui.adapter.holder.AddressSelectHolder;
import com.sctvcloud.dongqu.ui.adapter.holder.BaseAbsHolder;
import com.sctvcloud.dongqu.ui.util.IListShowData;

import java.util.List;

/**
 * Created by Liuyu on 2018/8/16.
 */

public class AddressSelectAdapter extends BaseHolderAbsAdapter<AddrInfo, BaseAbsHolder<AddrInfo>> {


    protected String selectId;
    protected int selectedPos = -1;
    protected AddAddressClick addressClick;

    public interface AddAddressClick {
        void onAddClick();

        void onSelectClick(AddrInfo info);
    }

    ;

    public void setAddAddressClick(AddAddressClick internalClick) {
        this.addressClick = internalClick;
    }

    public AddressSelectAdapter(Context context, List<AddrInfo> list) {
        super(context, list);
        this.canSelector = true;
    }

    @Override
    public BaseAbsHolder<AddrInfo> onCreateViewHolder(ViewGroup parent, int viewType) {
        if (viewType == 3) {
            View vv = LayoutInflater.from(context).inflate(R.layout.item_address_add, parent, false);
            AddressAddHolder aah = new AddressAddHolder(context, vv);
            aah.setInternalClick(this);
            return aah;
        } else {
            View v = LayoutInflater.from(context).inflate(R.layout.item_address, parent, false);
            AddressSelectHolder ash = new AddressSelectHolder(context, v);
            ash.setInternalClick(this);
            return ash;
        }
    }

    @Override
    protected boolean isSelected(AddrInfo iListShowData, int position) {
        return TextUtils.equals(iListShowData.getDataId(), selectId);
    }

    @Override
    public void onItemInternalClick(View itemView, View view, int position) {
        IListShowData isd = getItem(position);
        if (isd.getDataType() == 3) {
//            context.startActivity(new Intent(context, AddressAddActivity.class));
            if (addressClick != null) {
                addressClick.onAddClick();
            }
        } else {
            boolean b = TextUtils.equals(isd.getDataId(), selectId);
            selectId = isd.getDataId();
            if (!b) {
                notifyItemChanged(position);
                if (selectedPos >= 0) {
                    notifyItemChanged(selectedPos);
                }
            }
            selectedPos = position;
        }
        if (addressClick != null) {
            addressClick.onSelectClick(data.get(position));
        }
        super.onItemInternalClick(itemView, view, position);
    }

    public IListShowData getSelectedData() {
        if (selectedPos < 0 || selectedPos >= getItemCount()) {
            return null;
        }
        return getItem(selectedPos);
    }

    @Override
    public int getItemViewType(int position) {
        return data.get(position).getDataType();
    }
}
